﻿var itemSuggest = function () {
    /**
     * 请求json格式的数据 {value: [key:value,key2:value2]};
     */
    var IS_IdField, IS_keyField, domID, dataList = { value: [] }, dataNameList = { value: [] }, ShowBtn = true;
    //无参数传递的
    this.initSuggest = function (_domID, control, action, _idField, _keyField) {
        IS_IdField = _idField;
        IS_keyField = _keyField;
        domID = _domID;
        Y.API3(control, action, {}, function (json) {
            if (!json || json.length === 0) {
                return false;
            } else {
                var index, len;
                len = json.length;

                for (index = 0; index < len; index++) {
                    dataList.value.push({
                        IS_IdField: json[index][IS_IdField],
                        IS_keyField: json[index][IS_keyField]
                    });
                    dataNameList.value.push({
                        IS_keyField: json[index][IS_keyField]
                    });
                }
                dataList.defaults = 'wuweihua';
            }
        });

        $("#" + domID).bsSuggest({
            allowNoKeyword: false,   //是否允许无关键字时请求数据。为 false 则无输入时不执行过滤请求
            multiWord: true,         //以分隔符号分割的多关键字支持
            separator: ",",          //多关键字支持时的分隔符，默认为空格  
            keyLeft: 37,                    //向左方向键
            keyUp: 38,                      //向上方向键
            keyRight: 39,                   //向右方向键
            keyDown: 40,                    //向下方向键
            keyEnter: 13,                   //回车键
            data: dataNameList,
            showBtn: ShowBtn
        }).on('onDataRequestSuccess', function (e, result) {
            console.log('从 json.data 参数中获取，不会触发 onDataRequestSuccess 事件', result);
        }).on('onSetSelectValue', function (e, keyword, data) {
            console.log('onSetSelectValue: ', keyword, data);
        }).on('onUnsetSelectValue', function () {
            console.log("onUnsetSelectValue");
        });
    }
    this.initSuggestPram = function (_domID, control, action, _idField, _keyField, _Pram) {
        IS_IdField = _idField;
        IS_keyField = _keyField;
        domID = _domID;
        Y.API3(control, action, _Pram, function (json) {
            if (!json || json.length === 0) {
                return false;
            } else {
                var index, len;
                len = json.length;

                for (index = 0; index < len; index++) {
                    dataList.value.push({
                        IS_IdField: json[index][IS_IdField],
                        IS_keyField: json[index][IS_keyField]
                    });
                    dataNameList.value.push({
                        IS_keyField: json[index][IS_keyField]
                    });
                }
                dataList.defaults = 'wuweihua';
            }
        });

        $("#" + domID).bsSuggest({
            allowNoKeyword: false,   //是否允许无关键字时请求数据。为 false 则无输入时不执行过滤请求
            multiWord: true,         //以分隔符号分割的多关键字支持
            separator: ",",          //多关键字支持时的分隔符，默认为空格  
            keyLeft: 37,                    //向左方向键
            keyUp: 38,                      //向上方向键
            keyRight: 39,                   //向右方向键
            keyDown: 40,                    //向下方向键
            keyEnter: 13,                   //回车键
            data: dataNameList,
            showBtn: ShowBtn
        }).on('onDataRequestSuccess', function (e, result) {
            console.log('从 json.data 参数中获取，不会触发 onDataRequestSuccess 事件', result);
        }).on('onSetSelectValue', function (e, keyword, data) {
            console.log('onSetSelectValue: ', keyword, data);
        }).on('onUnsetSelectValue', function () {
            console.log("onUnsetSelectValue");
        });
    }
    this.getSelectIds = function () {
        var sel_val = $("#" + domID).val();
        var sel_item = [];
        if (sel_val != null) {
            var sel_vals = sel_val.split(",");
            if (sel_vals == null) {
                sel_vals = sel_val.split(" ");
            }
            if (sel_vals != null) {
                for (var i = 0; i < sel_vals.length; i++) {
                    var c = sel_vals[i];
                    for (var j = 0; j < dataList.value.length; j++) {
                        var d = dataList.value[j];
                        if (c === d.IS_keyField) {
                            sel_item.push(d.IS_IdField);
                        }
                    }
                }
            }
        }
        return sel_item.join(',');
    }
    this.destroy = function () {
        $("#" + domID).bsSuggest("destroy");
    }
    this.SetBtnHide = function () {
        ShowBtn = false;
    }

}